use serde::Deserialize;
use std::fmt::Write;

/// Redis配置项
#[derive(Debug, Deserialize, Clone)]
pub struct RedisConfig {
    /// redis类型
    pub redis_type: String,
    /// redis连接服务地址
    pub host: String,
    /// redis连接端口
    pub port: u64,
    /// redis连接密码
    pub password: Option<String>
}

/// Redis配置方法
impl RedisConfig {
    /// 获取redis连接地址  例：redis://:123456@127.0.0.1:6379
    pub fn get_url(&self) -> String {
        // format!("redis://{host}:{port}", host = self.host, port = self.port)
        let mut format = String::new();
        write!(format, "redis://{}", "").unwrap();

        if let Some(val) = &self.password {
            write!(format, ":{}@", &val).unwrap();
        }

        write!(format, "{}:{}", self.host, self.port).unwrap();
        log::info!("get redis url -> {}", &format);
        format
    }
}
